package com.healthbox.resultsservice.service.impl;

import com.healthbox.resultsservice.entity.*;
import com.healthbox.resultsservice.mapper.TOadminResultsMapper;
import com.healthbox.resultsservice.service.TOadminResultsService;
import com.healthbox.resultsservice.utils.MyTime;
import com.healthbox.resultsservice.utils.SendMQMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TOadminResultsServiceImpl implements TOadminResultsService {

    @Resource
    TOadminResultsMapper tOadminResultsMapper;

    @Resource
    SendMQMessage sendMQMessage;

    @Override
    public CommonResult create(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long AIId = Long.valueOf(map.get("ai_id").toString());
        String phoneNumber = tOadminResultsMapper.getUserPhone(AIId);
        if (phoneNumber == null) {
            return new CommonResult<>(500, "该预约编号不存在");
        }
        String result = (String) map.get("result");
        TestResult testResult = new TestResult();
        testResult.setTime(MyTime.getCurrTimeAsDate());
        testResult.setResult(result);
        int resCode = tOadminResultsMapper.create_TR(testResult);
        if (resCode != 1) {
            return new CommonResult<>(500, "创建错误");
        }
        Long TRId = testResult.getTRId();
        AL_TR al_tr = new AL_TR();
        al_tr.setAIId(AIId);
        al_tr.setTRId(TRId);
        resCode = tOadminResultsMapper.create_AL_TR(al_tr);
        if (resCode != 1) {
            return new CommonResult<>(500, "创建错误");
        }
        sendMQMessage.send(phoneNumber);
        return new CommonResult<>(200, "创建成功");
    }

    @Override
    public CommonResult delete(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long TRId = Long.valueOf(map.get("tr_id").toString());
        int resCode = tOadminResultsMapper.delete(TRId);
        if (resCode != 1) {
            return new CommonResult<>(500, "删除错误");
        }
        return new CommonResult<>(200, "删除成功");
    }

    @Override
    public CommonResult update(Map<String, Object> map, String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long TRId = Long.valueOf(map.get("tr_id").toString());
        Long AIId = Long.valueOf(map.get("ai_id").toString());
        String result = (String) map.get("result");
        int resCode = tOadminResultsMapper.update(AIId, TRId, result);
        if (resCode != 1) {
            return new CommonResult<>(500, "修改失败");
        }
        return new CommonResult(200, "修改成功");
    }

    @Override
    public CommonResult retrieve(String userPhone, String userRole) {
        if (userRole == "user") {
            return new CommonResult<>(403, "权限不足");
        }
        Long hospital_id = tOadminResultsMapper.checkHospital(userPhone);
        List<TestResultForTOadmin> origin = tOadminResultsMapper.retrieve(hospital_id);
        List<TestResultForTOadminVue> res = new ArrayList<>();
        for(TestResultForTOadmin item: origin) {
            TestResultForTOadminVue testResultForTOadminVue = new TestResultForTOadminVue();
            testResultForTOadminVue.setResult(item.getResult());
            testResultForTOadminVue.setAi_id(item.getAIId());
            testResultForTOadminVue.setPhoneNumber(item.getUser_phone());
            testResultForTOadminVue.setTr_id(item.getTRId());
            testResultForTOadminVue.setTime(MyTime.dateTime2String(item.getTime()));
            res.add(testResultForTOadminVue);
        }
        if (res.size() >= 1) {
            return new CommonResult<>(200, "查看成功", res);
        } else {
            return new CommonResult<>(200, "暂无数据", res);
        }
    }
}
